/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.nonmember;

import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.ShoppingCart;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import service.ProductServiceLocal;

/**
 *
 * @author SONY
 */
public class UpdateCartStrutsAction extends org.apache.struts.action.Action {
    
    @EJB ProductServiceLocal productServiceLocal;

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String errorMsg = "";
        try {
            String productID = request.getParameter("ProductID");
            String quantity = request.getParameter("quantity");
            String quantityExpr = "([0-9]+)";
            if (quantity.length() == 0) {
                errorMsg = "Giá trị nhập vào không hợp lệ!";
            } else if(!quantity.matches(quantityExpr)) {
               errorMsg = "Giá trị nhập vào không hợp lệ!";
            } else {
                //get ShoppingCart object in the session-scope to add ItemCart object to shopping cart
                HttpSession session = request.getSession();
                ShoppingCart shopping_cart = (ShoppingCart) session.getAttribute("shopping_cart");
                int qty = Integer.parseInt(quantity);
                if(qty == 0) {
                    shopping_cart.deleteItemCart(productID);
                    return mapping.findForward(SUCCESS);
                } else {
                    ServletContext context = servlet.getServletContext();
                    productServiceLocal = (ProductServiceLocal) context.getAttribute("productServiceLocal");
                    if(productServiceLocal.checkQOH_byPID(productID, qty)) {
                        shopping_cart.updateItemCart(productID, qty);
                        return mapping.findForward(SUCCESS);
                    } else {
                        errorMsg = "Số lượng mua đã vượt mức số lượng hàng tồn kho!";
                    }
                }
            }
            
        } catch (Exception e) {
            System.err.println(e);
        }
        request.setAttribute("errorMsg", errorMsg);
        return mapping.findForward("error");

    }
}
